//
//  YHEmotionView.swift
//  XLWB
//
//  Created by key on 16/7/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class YHEmotionView: UIView {

    //MARK: - 入口
    override init(frame: CGRect) {
        super.init(frame:CGRect(x: 0, y: 0, width: screenW, height: 216))
        
        backgroundColor = themeColor
        
        //加载子控件
        addChildView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 加载子控件
    //懒加载 bar
    var emotionBar: YHEmotionBarView = {
        
        let emo = YHEmotionBarView()
        
        return emo
    }()
    
    //懒加载表情
    var emotionImageCol: YHEmotionCollectionView = {
        
        let imgCol = YHEmotionCollectionView()
        
        imgCol.bounces = false
        
        imgCol.showsHorizontalScrollIndicator = false
        
        imgCol.pagingEnabled = true
        
        return imgCol
    }()
    
    //懒加载pageControl
    lazy var pageCon: UIPageControl = {
        
        let page = UIPageControl()
        
        page.currentPage = 0
        
        page.numberOfPages = YHEmotionData.shareEmotion.emotionAllArray[0].count//默认表情数组页数
        
        
        //选中状态
//        page.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //KVC 设置
        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        //默认状态
//        page.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        //KVC 设置
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
        
        return page
        
    }()
    
    //添加子控件并约束
    func addChildView() {
    
        //添加
        addSubview(emotionBar)
        addSubview(emotionImageCol)
        addSubview(pageCon)
        
        //设置图片collectionView代理
        emotionImageCol.delegate = self
        
        //实现闭包
        closure()
        
        //约束
        emotionBar.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emotionImageCol.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emotionBar.snp_top)
            
        }
        
        pageCon.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(emotionImageCol)
            make.centerX.equalTo(emotionImageCol)
        }
        
    }
    
    
    //MARK: - 闭包相关
    func closure() {
        
        //bar 点击响应事件
        emotionBar.emotionBarClosure = {[weak self] (type: YHEmotionBarViewType) -> () in

            //获取点击button的tag 滚动到指定cell
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self!.emotionImageCol.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            //设置pagecontrol当前页
            //当前页
            self!.pageCon.currentPage = indexPath.item
            //总页数
            self!
                .pageCon.numberOfPages = YHEmotionData.shareEmotion.emotionAllArray[indexPath.section].count

        }
        
    }
    
    
    
    
    
    
}


extension YHEmotionView: UICollectionViewDelegate {
    
    //MARK: - 实现图片collectionView代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //获取屏幕中心 在collentionView 中的相对位置  根据偏移量进行计算
        let centerX = scrollView.contentOffset.x + screenW / 2
        
        
        //----->
        //获取collectionView中显示的cell 当cell 的size和屏幕是一样宽的时候 同时只会显示两个 cell  这个时候 就可以使用这种方法  但是顺序是无序的  所以需要进行排序 利用sort 进行排序
        let cells =  emotionImageCol.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //----->
        
        //判断 如果当前屏幕上面不是两个cell 时  直接返回
        if cells.count != 2 {
            
            return
        }
        //获取前一个cell
        let proCell = cells.first!
        //获取后一个cell
        let nextCell = cells.last!
        
        //判断那个cell包含屏幕中心 从而判断哪个cell在当前屏幕占用的分量大
        var indexPath: NSIndexPath
        if proCell.frame.contains(CGPoint(x: centerX, y: 0)) {
            
            //获取当前cell 的indexPath
            indexPath = emotionImageCol.indexPathForCell(proCell)!
            
        }else{
            
            //获取当前cell 的indexPath
            indexPath = emotionImageCol.indexPathForCell(nextCell)!
            
        }
        
        //根据cell所在组 判断YHEmotionBarView button 应该在选中哪一个
        emotionBar.setSelectedButton(indexPath.section)
        
        
        //MAKR: - 设置pagecontrol当前页
        //当前页
        pageCon.currentPage = indexPath.item
        //总页数
        pageCon.numberOfPages = YHEmotionData.shareEmotion.emotionAllArray[indexPath.section].count
        
        
        
    }
    
}
